We have already used few of the functions in Python, such as, len
, random
, range
, etc.
Functions could be considered a black box object, which given some input gives back some output. So for example, len takes an object as its input and returns its length as output.
In [1]:
def get_simple_interest(p, r, t):
return p * r * t / 100.0
In [5]:
get_simple_interest(10000, 10, 1)
Out[5]:
In [6]:
def get_perimeter_and_area(r):
perimieter = 2 * 3.14 * r
area = 3.14 * r ** 2
return perimieter, area
In [8]:
perimeter, area = get_perimeter_and_area(10)
print 'Perimeter = ' + str(perimeter)
print 'Area = ' + str(area)
You can return any number of values from a function and read them in that order when you call it
Python wraps these multiple return values in the form of a tuple and returns to the caller